var crtBaseID="n.tbPlanType.ptId";

$(document).ready(function () {
	//findAllPlanType();
	findAllPlanGrade();
	$("#planKind").change(function () {
		$("#loadDIV").hide();
		$("#loading").show();
		listPlan();
	});
});

function planKindClass(){
	this.name="";
	this.id="";
}

function planCtnClass(){
	this.pname="";
	this.pid="";
	this.pkid="";
	this.pindex=0;
}

/**
 * 作获取所有预案所有记录数操作
 */
function findPlanNoReportRC(){
	var url = 'lzm/Plan_findAllPlanRC.action';
	var params = {
			"queryOption['planType']":0,
			"queryOption['planStatus']":0,
			"queryOption['planScope']":0,
			"queryOption['planRegion']":0,
			"queryOption['planGrade']":0,
			planStatus:RELEASE
	}; 
	jQuery.post(url, params, findPlanNoReportRCRst, 'json');
}

/**
 * 作获取预案所有记录数回调函数
 */
function findPlanNoReportRCRst(data){
	findPartByPage(1,data.recordCount);
}

/**
 * 发送异步请求获取分页数据
 */
function findPartByPage(first,max){
	var url = 'lzm/Plan_findAllPlan.action';
	var params = {
			firstRow : first-1,
			maxRow : max,
			"queryOption['planType']":0,
			"queryOption['planStatus']":0,
			"queryOption['planScope']":0,
			"queryOption['planRegion']":0,
			"queryOption['planGrade']":0,
			planStatus:RELEASE
	}; 
	jQuery.post(url, params, findPartByPageRst, 'json');
}

/**
 * 发送异步请求获取分页数据回调函数
 */
function findPartByPageRst(planData){
	if(planData.ajaxRst==SUCCESS){
		displayPlan(planData);
	}else{
		alert("服务器忙,请稍候再试！");
	}
}

function findPlanById(aaa){	
	//setCookie(PId);
	setCookie(PId,aaa);
	//window.parent.frames['mainFrame'].reload();
	window.parent.frames['mainFrame'].location.reload();
/*
	var timestamp = Date.parse(new Date());
	$.get("lzm/Plan_findPlanById.action?ts="+timestamp+"&PId="+aaa,null,findPlanByIdRst);
*/
}

function dom4frame(domm){
	return $(domm,window.parent.frames['mainFrame'].document);
}

function findPlanByIdRst(data){
	window.parent.frames['mainFrame'].reload();
	/*
	var plan=data.tbPlan;
	dom4frame('#PName').text(plan.PName);
	dom4frame('#ptName').text(plan.tbPlanType.ptName);
	dom4frame('#pgName').text(plan.tbPlanGrade.pgName);
	dom4frame('#etEventType').text(plan.tbEventType.etEventType);
	dom4frame('#psName').text(plan.tbPlanScope.psName);
	dom4frame('#prName').text(plan.tbPlanRegion.prName);
	dom4frame('#PSummary').text(plan.PSummary);
	dom4frame('#PUnit').text(plan.PUnit);
	dom4frame('#PContractor').text(plan.PContractor);
	dom4frame('#PTime').text(plan.PTime.replace(/T/," "));
	dom4frame('#PVersion').text(plan.PVersion);*/
	//时间管理
	//dom4frame('#ptName').text(plan.PVersion);
	
	/*
	alert("dfgdfgd");
	alert(dom4frame('#PName').html());
	alert(plan.PName);
	*/
	/*
	
	$('#PName').text(plan.PName);
	$('#planType').text(plan.tbPlanType.ptName);
	$('#planGrade').text(plan.tbPlanGrade.pgName);
	$('#eventType').text(plan.tbEventType.etEventType);
	$('#planScope').text(plan.tbPlanScope.psName);
	$('#planRegion').text(plan.tbPlanRegion.prName);
	$('#PSummary').text(plan.PSummary);
	$('#PUnit').text(plan.PUnit);
	$('#PContractor').text(plan.PContractor);
	$('#PTime').text(plan.PTime.replace(/T/," "));
	$('#PVersion').text(plan.PVersion);	*/
}

function displayPlan(planData){
	$.each(planData.planList, function (i, n) {
		var tmp=eval(crtBaseID);
		$("#"+tmp+" ul").append(generalPlan(n.PId,n.PName));
	});
	//$("#navigation").treeview();
	$("#navigation").treeview({
		persist: "location",
		collapsed: false,
		unique: true
	});
	$("#loading").hide();
	$("#loadDIV").show();
	$('.pcol').click(function () {
		//alert($(this).children(".pcoc").text());
		findPlanById($(this).children(".pcoc").text());
	});	
	$('.pcol')[0].click();
}

function generalPlan(planID,planName){
	var strhtml="<li class='pcol'>";
	strhtml+=planName;
	strhtml+="<span class='pcoc'>"
	strhtml+=planID;
	strhtml+="</span></li>";
	return strhtml;
}


/**
 * 开始显示预案，首先开始读取预案下拉框的选中情况
 */
function listPlan() {
	var pySlt=$("#planKind").val();
	switch(pySlt){
		case '1':
			disPlanKind("n.tbPlanType.ptId","n.tbPlanType.ptName",findAllPlanType);//预案类型
			break;	
		case '2':
			disPlanKind("n.tbPlanStatus.pstId","n.tbPlanStatus.pstName",findAllPlanStatus);//预案状态
			break;
		case '3':
			disPlanKind("n.tbPlanScope.psId","n.tbPlanScope.psName",findAllPlanScope);//适用范围
			break;	
		case '4':
			disPlanKind("n.tbPlanRegion.prId","n.tbPlanRegion.prName",findAllPlanRegion);//适用区域
			break;	
		case '5':
			disPlanKind("n.tbPlanGrade.pgId","n.tbPlanGrade.pgName",findAllPlanGrade);//预案等级
			break;	
		case '6':
			disPlanKind("n.tbEventType.etId","n.tbEventType.etEventType",findAllEventType);//事件类型
			break;	
		default:
			disPlanKind("n.tbPlanType.ptId","n.tbPlanType.ptName",findAllPlanType);	//预案类型
	}
}

/**
 * 开始显示预案下拉框的显示页面显示
 * @param {Object} pkID
 * @param {Object} pkName
 */
function disPlanKind(pkID,pkName,planFun){
	crtBaseID=pkID;
	planFun();
}
/**
 * 作获取所有事件类型操作
 */
function findAllEventType() {
	var timestamp = Date.parse(new Date());
	$.get("lzm/FindPlanBasis_findAllEventType.action?ts=" + timestamp, null, findAllEventTypeRst);
}
/**
 * 作获取所有预案类型操作
 */
function findAllPlanType() {
	var timestamp = Date.parse(new Date());
	$.get("lzm/FindPlanBasis_findAllPlanType.action?ts=" + timestamp, null, findAllPlanTypeRst);
}
/**
 * 作获取所有预案状态操作
 */
function findAllPlanStatus() {
	var timestamp = Date.parse(new Date());
	$.get("lzm/FindPlanBasis_findAllPlanStatus.action?ts=" + timestamp, null, findAllPlanStatusRst);
}
/**
 * 作获取所有预案适用范围操作
 */
function findAllPlanScope() {
	var timestamp = Date.parse(new Date());
	$.get("lzm/FindPlanBasis_findAllPlanScope.action?ts=" + timestamp, null, findAllPlanScopeRst);
}
/**
 * 作获取所有预案适用区域操作
 */
function findAllPlanRegion() {
	var timestamp = Date.parse(new Date());
	$.get("lzm/FindPlanBasis_findAllPlanRegion.action?ts=" + timestamp, null, findAllPlanRegionRst);
}
/**
 * 作获取所有预案等级操作
 */
function findAllPlanGrade() {
	var timestamp = Date.parse(new Date());
	$.get("lzm/FindPlanBasis_findAllPlanGrade.action?ts=" + timestamp, null, findAllPlanGradeRst);
}
/**
 * 作获取事件类型回调函数
 */
function findAllEventTypeRst(data) {
	var strhtml="";
	$.each(data.eventTypeList, function (i, n) {
		strhtml+=generalNode(n.etId,n.etEventType);		
	});
	$('#treeRoot').children().remove();
	$('#treeRoot').append(strhtml);
	findPlanNoReportRC();
}
/**
 * 作获取预案类型回调函数
 */
function findAllPlanTypeRst(data) {
	var strhtml="";
	$.each(data.planTypeList, function (i, n) {
		strhtml+=generalNode(n.ptId,n.ptName);		
	});
	$('#treeRoot').children().remove();
	$('#treeRoot').append(strhtml);
	findPlanNoReportRC();
}
/**
 * 作获取预案状态回调函数
 */
function findAllPlanStatusRst(data) {	
	var strhtml="";
	$.each(data.planStatusList, function (i, n) {
		strhtml+=generalNode(n.pstId,n.pstName);		
	});
	$('#treeRoot').children().remove();
	$('#treeRoot').append(strhtml);
	findPlanNoReportRC();
}
/**
 * 作获取预案适用范围回调函数
 */
function findAllPlanScopeRst(data) {	
	var strhtml="";
	$.each(data.planScopeList, function (i, n) {
		strhtml+=generalNode(n.psId,n.psName);		
	});
	$('#treeRoot').children().remove();
	$('#treeRoot').append(strhtml);
	findPlanNoReportRC();
}
/**
 * 作获取预案适用区域回调函数
 */
function findAllPlanRegionRst(data) {
	var strhtml="";
	$.each(data.planRegionList, function (i, n) {
		strhtml+=generalNode(n.prId,n.prName);		
	});
	$('#treeRoot').children().remove();
	$('#treeRoot').append(strhtml);
	findPlanNoReportRC();
}
/**
 * 作获取预案等级回调函数
 */
function findAllPlanGradeRst(data) {
	var strhtml="";
	$.each(data.planGradeList, function (i, n) {
		strhtml+=generalNode(n.pgId,n.pgName);		
	});
	$('#treeRoot').children().remove();
	$('#treeRoot').append(strhtml);
	findPlanNoReportRC();
}
/**
 * 生成预案第二层节点
 * @param {Object} pkID
 * @param {Object} pkName
 */
function generalNode(pkID,pkName){
	var strhtml="<li id="+pkID;
	strhtml+="><span>";
	strhtml+=pkName;
	strhtml+="</span><ul></ul></li>"
	return strhtml;
}

